﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using ZombieMonkeys.Vibria.Logic;

namespace ZombieMonkeys.Vibria.DAL
{

    /// <summary>
    /// A class that will read in an xml file and load the data as a sudoku puzzle
    /// </summary>
    public class PuzzleLoader
    {
        #region fields

        static PuzzleLoader instance;

        #endregion fields

        #region Constructors

        private PuzzleLoader()
        {

        }

        #endregion Constructors

        #region Properties

        #endregion Properties

        #region PublicMethods

        public static PuzzleLoader GetListLoader()
        {
            if (instance == null)
            {
                instance = new PuzzleLoader();
            }
            return instance;
        }
        /// <summary>
        /// This will return all the Square data that was saved in a puzzle xml file
        /// </summary>
        /// <param name="vibriaSavedPuzzleToLoad"></param>
        /// <returns></returns>
        public Puzzle LoadPuzzle(string vibriaSavedPuzzleToLoad)
        {
            Puzzle tempPuzzle;
            List<Square> tempSquares = new List<Square>();

            XDocument puzzleToLoad = XDocument.Load(vibriaSavedPuzzleToLoad);

            tempPuzzle = parsePuzzleData(puzzleToLoad);
            return tempPuzzle;
        }

        #endregion PublicMethods

        #region privateMethods

        public void LoadPuzzleTimeData(string vibriaSavedPuzzleToLoad)
        {
            //pass in the directory for the file and get the XDoc
            XDocument xDoc = XDocument.Load(vibriaSavedPuzzleToLoad);
            //this will return something that contains all the data about the current time
            //spent on a puzzle as of saving
        }

        private void parsePuzzleTimeData(XDocument xDoc)
        {
            //xDoc.Root.Element("ts"
        }

        private Puzzle parsePuzzleData(XDocument xDoc)
        {
            List<Square> tempSquares = new List<Square>();
            IEnumerable<XElement> squareXElements;

            int rows = 0;
            int cols = 0;
            string r = "";
            string c = "";
            string difficulty = "";

            XElement puzzleXElement = xDoc.Root;//.Element("puzzle");
            rows = tryParseValue(puzzleXElement.Attribute("rows").Value);
            cols = tryParseValue(puzzleXElement.Attribute("rows").Value);
            difficulty = puzzleXElement.Attribute("diff").Value;

            Square tempSquare;
            //should be about 81, if a 9 x 9 grid is assumed
            squareXElements = xDoc.Root.Element("values").Elements("s");
            int count = 0;
            count = squareXElements.Count();
            foreach (XElement squareXElement in squareXElements)
            {
                tempSquare = new Square();
                tempSquare.SetRow(tryParseValue(squareXElement.Attribute("r").Value));
                tempSquare.SetColumn(tryParseValue(squareXElement.Attribute("c").Value));
                tempSquare.SetPermanent(tryParseBool(squareXElement.Attribute("p").Value));
                tempSquare.SetValue(tryParseValue(squareXElement.Attribute("v").Value));
                tempSquare.SetNote(0, tryParseValue(squareXElement.Attribute("n1").Value));
                tempSquare.SetNote(1, tryParseValue(squareXElement.Attribute("n2").Value));
                tempSquare.SetNote(2, tryParseValue(squareXElement.Attribute("n3").Value));
                tempSquare.SetNote(3, tryParseValue(squareXElement.Attribute("n4").Value));

                tempSquares.Add(tempSquare);
            }
            //build the puzzle now

            #region oldCode
            #region usedalready
            //List<Square> tempSquares = new List<Square>();
            //int rows = 0;
            //int cols = 0;
            //string difficulty;

            //int rowId = 0;
            //int colId = 0;
            ////rows = xDoc.Root.Element("puzzle").Attribute("rows").Value;
            ////cols = xDoc.Root.Element("puzzle").Attribute("cols").Value;
            //difficulty = xDoc.Element("puzzle").Attribute("diff").Value;

            ////rowId = xDoc.Root.Element("values").Element("r").Attribute("id").Value;
            //IEnumerable<XElement> squareXElements;
            //IEnumerable<XElement> rowElements;

            //squareXElements = xDoc.Root.Element("values").Elements("s");
            #endregion usedalready

            ////for each row element in the doc
            //Square square;
            //foreach (XElement rowXElement in rowElements)
            //{
            //    //get all the of squares in each row
            //    squareXElements = rowXElement.Elements("s");

            //    foreach (XElement squareXElement in squareXElements)
            //    {
            //        square = new Square();

            //        //set up the logic to get the values back right
            //        square.SetRow(tryParseValue(rowXElement.Attribute("id").Value));
            //        //get the square col value
            //        square.SetColumn(tryParseValue(squareXElement.Attribute("c").Value));

            //        //get the square permanent value
            //        square.SetPermanent(tryParseBool(squareXElement.Attribute("p").Value));

            //        //get the square value
            //        square.SetValue(tryParseValue(squareXElement.Attribute("v").Value));

            //        //get the square notes each individually, passing in first value as the index in the array, you want
            //        square.SetNote(0, tryParseValue(squareXElement.Attribute("n1").Value));
            //        square.SetNote(1, tryParseValue(squareXElement.Attribute("n2").Value));
            //        square.SetNote(2, tryParseValue(squareXElement.Attribute("n3").Value));
            //        square.SetNote(3, tryParseValue(squareXElement.Attribute("n4").Value));

            //        tempSquares.Add(square);
            //    }
            //}

            #endregion oldCode

            Grid tempGrid = new Grid(tempSquares);
            Puzzle tempPuzzle = new Puzzle(tempGrid);
            tempPuzzle.Cols = cols;
            tempPuzzle.Rows = rows;
            tempPuzzle.Difficulty = difficulty;

            //save current time add to xml changes as ct h= m= s=
            //load the date time stamp in a different call
            return tempPuzzle;
        }

        private bool tryParseBool(string boolString)
        {
            bool b = false; //; = Convert.ToBoolean(boolString);
            b = Boolean.TryParse(boolString, out b);
            return b;
        }

        private int tryParseValue(string intString)
        {
            int value = 0;
            value = int.Parse(intString);
            return value;
        }

        #endregion privateMethods
        //we are going to have to come up with a format for our xml data and make it in such a way that it's easy to load whatever the state of the puzzle
    }
}
